package com.icim.twqc.exporter.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.ServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.icim.core.service.IAlarmSenderService;
import com.icim.twqc.exporter.service.ITwqcExportShippingDataService;
import com.icim.twqc.exporter.service.TwqcExportShippingDataService;
import com.icim.twqc.exporter.tx.TwqcShippingO;
import com.icim.util.XmlConvertUtil;

@Controller
public class TwqcShippingdataController {

	    @Resource
	    private ITwqcExportShippingDataService ItwqcExportShippingDataService;
	    
	    @Resource
	    private TwqcExportShippingDataService twqcExportShippingDataService;
	    
	    @Resource
	    private IAlarmSenderService alarmSenderService;
	    
	    private Logger logger = LoggerFactory.getLogger(TwqcShippingdataController.class);

	    @ResponseBody
	    @RequestMapping("exportTwqcShippingdata.do")
	    public void exportShippingData(String palletId,ServletResponse response) throws IOException{

	        TwqcShippingO outTrx1 = new TwqcShippingO();
	        TwqcShippingO outTrx2 = new TwqcShippingO();
	        String reply = null;
	        PrintWriter pw = response.getWriter();
	        //回传xml文件
	        try {
	        	 logger.info("232 palletId:"+palletId);
	        	 reply = twqcExportShippingDataService.subMainProc(palletId);
	        	 logger.info("====================================================");
		         logger.info("TWQC start shipping data .....");
		         outTrx2 = ItwqcExportShippingDataService.sendShippingData(palletId);
	        	 pw.write(reply);
	             pw.flush();
	        } catch (Exception e) {
	        	String errorMsg="栈板："+palletId+"  数据回传失败\n"+"失败原因："+e.getMessage();
	        	alarmSenderService.sendAlarm1("232",errorMsg);
	            outTrx1.setRtn_code("999999");
	            outTrx1.setRtn_mesg(e.toString());
	            reply= XmlConvertUtil.obj2Xml(outTrx1, "transaction");
	            pw.write(reply);
	            pw.flush();
	        }finally{
	            pw.close();
	        }
	        
	        
	        //回传defect、spc、箱子良率等信息
	        /*try {
	        	logger.info("====================================================");
	        	logger.info("TWQC start shipping data .....");
	            outTrx2 = ItwqcExportShippingDataService.sendShippingData(palletId);
	            //outTrx2.setRtn_code("0000000");
	            //outTrx2.setRtn_mesg("SUCCESS");
	        } catch (Exception ex) {
	        	String errorMsg = "栈板："+palletId+"  数据回传失败\n"+"失败原因："+ex.getMessage();
	        	alarmSenderService.sendAlarm1("232",errorMsg);
	            outTrx2.setRtn_code("9999999");
	            outTrx2.setRtn_mesg(ex.getMessage());
	           logger.info("error: {} " ,ex);
	        }*/
	        
	    }
}
